package com.glsc.ngateway.shortmsg.task;

import com.glsc.ngateway.common.base.repo.mysql.gateway.shortmsg.NoticeMessageSendRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class SendMsgExecutorService implements InitializingBean {


    @Value("${shortmsg.url}")
    public String SHORT_MESSAGE_URL;

    @Autowired
    NoticeMessageSendRepository noticeMessageSendRepository;

    @Autowired
    RestTemplate restTemplate;


    /**
     * 考虑到现在发送短信的量不太大，所以直接用一个线程处理即可，也比较简单
     * 如果是多线程要考虑mysql行级排他锁以及一些对象的线程安全等问题
     */
//    @Value("${sendMsgThreadPool.corePoolSize}")
    private int corePoolSize = 1;

    @Override
    public void afterPropertiesSet() throws Exception {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(corePoolSize);
        executor.scheduleWithFixedDelay(new SendMsgTask(noticeMessageSendRepository, restTemplate, SHORT_MESSAGE_URL), 5, 3, TimeUnit.SECONDS);
//        executor.scheduleWithFixedDelay(new SendMsgTask(),5,1, TimeUnit.SECONDS);
    }
}
